/**
 * python gen: Tue Jun  8 09:13:50 2021
 */
import { sysdictdetailService } from "../../../api/apiService";
export default {
  name: "sysdictdetailHome",
  props: {
    dictId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      options: [
        {
          value: "1",
          label: "通过"
        },
        {
          value: "2",
          label: "不通过"
        },
        {
          value: "0",
          label: "未检查"
        }
      ],
      searchForm: {
        // name: "",
        CHECK_RESULT: "",
        sort: ""
      },
      multipleSelection: [],
      tableData: [],
      currentPage: 1,
      totalCount: 0,
      PageSize: this.pageSizes[0],

      dialogFormVisible: false,
      ruleForm: {
        CHECK_ID: "",

        oid: "",

        dictId: "",

        label: "",

        value: "",

        dictSort: "",

        createBy: "",

        updateBy: "",

        createTime: "",

        updateTime: ""
      },
      rules: {
        CHECK_ID: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        oid: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        dictId: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        label: [{ required: true, message: "不能为空", trigger: "blur" }],

        value: [{ required: true, message: "不能为空", trigger: "blur" }],

        dictSort: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        createBy: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        updateBy: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        createTime: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ],

        updateTime: [
          // { required: true, message: "不能为空", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    // do sth
    this.getList(true);
  },
  // watch: {
  //   dictId(newValue, oldValue) {
  //     this.getList();
  //   }
  // },

  methods: {
    //todo

    handleSortChange({ prop, order }) {
      // console.log(column, prop, order);
      if (!order) {
        return;
      }
      if ("descending" === order) {
        this.searchForm.sort = `${prop},desc`;
      }
      if ("ascending" === order) {
        this.searchForm.sort = `${prop}`;
      }

      this.getList();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
      this.getList(true);
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      this.getList();
    },

    resetForm() {
      this.$refs["searchForm"].resetFields();
      this.getList();
    },
    getList(flag) {
      flag && (this.currentPage = 1);
      let params = {
        size: this.PageSize,
        page: this.currentPage - 1,
        // ...this.searchForm,
        dictId: this.dictId
      };
      sysdictdetailService.queryData(params).then(res => {
        let {
          data: { total, records }
        } = res;
        this.totalCount = total;
        this.tableData = records;
      });
    },

    // to 新增
    goAdd() {
      if (!this.dictId) {
        this.$message({
          message: `请选择对应的字典`,
          type: "error"
        });

        return;
      }
      Object.keys(this.ruleForm).forEach(key => (this.ruleForm[key] = ""));
      this.dialogFormVisible = true;
      this.ruleForm.dictId = this.dictId;

      //   this.$refs["ruleForm"].resetFields();
      //   this.$router.push({ name: "SYSDICTDETAIL-edit", params: { id: "" } });
    },

    // to 编辑
    goUpdate() {
      let item = this.multipleSelection[0];
      this.ruleForm = JSON.parse(JSON.stringify(item));
      this.dialogFormVisible = true;
    },

    // 新增，修改 后， 点击[保存]
    doSave() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          sysdictdetailService.postItem(this.ruleForm).then(() => {
            this.dialogFormVisible = false;
            this.getList();
          });
        }
      });
    },

    // to delete
    goDelete() {
      this.$confirm("确定要删除数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.doDelete();
        })
        .catch(() => {});
    },

    doDelete() {
      let ids = this.multipleSelection.map(it => it.oid);
      let params = { ids };
      sysdictdetailService.deleteItem(params).then(() => {
        this.getList();
      });
    },

    // to check
    // doCheck() {
    //   let params = { tables: "SYSDICTDETAIL" };
    //   apiService.checkItem(params).then((res) => {
    //     let { total, errors } = res;
    //     this.$message({
    //       message: `总扫描记录行数: ${total}, 发现错误数: ${errors} `,
    //       type: "success",
    //     });
    //     this.getList();
    //   });
    // },

    // to import
    goImport() {},
    // to export
    goExport() {
      // let params = {
      //   tableCode: "SYSDICTDETAIL",
      //   desensitize: true,
      // };
      // // apiService.exportItem(params);
      // let baseUrl = process.env.VUE_APP_BASE_URL;
      // window.open(
      //   `${baseUrl}/collect/packet/export?tableCode=${params.tableCode}&desensitize=${params.desensitize}`
      // );
    }
  }
};
